#
# Released under the MIT License : 
#
# Copyright (c) 2011 Manju Joseph
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#

#
# This program uses the pyfpdf library (http://code.google.com/pyfpdf/).
# The pyfdf library and its use are covered under the GNU LGPL v3, a copy
# of which can be found in the file LGPL in the licences/ directory. 
#

from fpdf import FPDF
import sys

width = 356;
length = 216;
margin = 10
topmargin = 5

one_copy_width = width/2 - (2 * margin)
left_width = one_copy_width * 92 / 150.0
right_width = one_copy_width - left_width

title1 = "FORM 5 [SEE RULE VI-17(1)]"
title2 = "TRANSFER CERTIFICATE"

name = sys.argv[2]
if len(name) == 0 :
    name = 'common-data'
common_entries = []
for line in file(name):
    common_entries.append((line[1:-1].split(':')[1]).strip())
    

firstrow_entries = ["NO. : ", "DATE OF ADMISSION : ", "ADMISSION NO. : "]
main_entries = ["NAME OF THE SCHOOL", \
        "WHETHER THE SCHOOL IS AIDED, GOVT OR RECOGNIZED", \
         "NAME OF THE PUPIL", \
        "NAME OF PARENT/GUARDIAN & RELATIONSHIP OF THE PUPIL\n TO THE GUARDIAN", \
        "IDENTIFICATION MARKS IF ANY OF THE PUPIL", \
        "NATIONALITY", \
        "COMMUNITY AND RELIGION", \
        "WHETHER THE CANDIDATE BELONGS TO SC OR ST OR OTHER OBC OR WHETHER HE IS CONVERT FROM THE SC OR ST", \
        "DATE OF BIRTH ACCORDING TO ADMISSION REGISTER (IN WORDS)", \
        "STANDARD IN WHICH THE PUPIL WAS LAST ENROLLED", \
        "DATE OF ADMISSION OR PROMOTION TO THAT STANDARD",
        "WHETHER QUALIFIED FOR PROMOTION TO A HIGHER STANDARD", \
        "WHETHER THE PUPIL HAS PAID ALL THE FEES DUE TO THE SCHOOL", \
        "WHETHER THE PUPIL WAS IN RECEIPT OF FEE CONCESSION", \
        "DATE OF THE PUPIL\'S LAST ATTENDANCE AT SCHOOL", \
        "DATE ON WHICH THE PUPIL\'S NAME WAS REMOVED FROM ROLLS", \
        "DATE APPLICATION FOR CERTIFICATE", \
        "DATE OF ISSUE OF CERTIFICATE", \
        "REASON OF LEAVING", \
        "SCHOOL TO WHICH THE PUPIL INTENDS TO PROCEEDING", \
        "DATE OF LAST SUCCESSFUL VACCINATION", \
        "NUMBER SCHOOL DAYS UP TO THE DATE", \
        "NUMBER SCHOOL DAYS PUPIL ATTENDED"]
place = "PLACE : "
date = "DATE : "
signatory = common_entries[16]

pdf = FPDF('L', 'mm', 'Legal')
pdf.set_margins(margin,5,-1 * margin)

#2: $20822     :     Admission #
#3:$SHAMNAS. V.K     :     Name
#10:$25/05/1993     :     DOB
#11: $04         :    Religion ?( If so, ISLAM, 01 HINDU 02 CHRISTIAN 04 ISLAM 03 MUSLIM 05 JAIN, Anything else?)
# 12: $4         :     (1 is genarl 2 is sc 3 is st 4 is obc 5 is oec)
#13: $MOPILA     :     Cast
#14 : $I         :     Don't know same for all, Indian?    
# 15 : $MOIDEEN KUTTY. V.K :     Guardian
#16 : $MOIDEEN KUTTY. V.K :     Father
#17 : $FATHIMA     :     Mother
#$U         :     Don't know
#$M         :     Don't know, same for all
#$H         :     Don't know, same for all
#$F         :     Don't know
#$A BLACK MOLE ON THE CHEST     :     ID Mark
#$10         :     Don't know, 10 for all
#$P         :     Don't know, P for all
#$100806     :     Don't know
#$10000.00     :     Don't know
#$04         :     Don't know
#$1         :     Don't know, 1 for all
#$         :     Don't know, empty for all

count = 0
name = sys.argv[1]
for line in file(name):
    count = count + 1
    tcnumber = str(count)
    data = line[1:-1].split('$')
    religions = {
            '01' : 'HINDU', '02' : 'CHRISTIAN', '04' : 'ISLAM', '03' : 'MUSLIM' , '05' : 'JAIN',
            '1' : 'HINDU', '2' : 'CHRISTIAN', '4' : 'ISLAM', '3' : 'MUSLIM' , '5' : 'JAIN'
               }
    religion = religions[data[10]]

    categories = { 
        '1' : 'GENERAL' , '2' : 'SC', '3' : 'ST', '4' : 'OBC', '5' : 'OEC',
        '01' : 'GENERAL' , '02' : 'SC', '03' : 'ST', '04' : 'OBC', '05' : 'OEC'
             }
    category = categories[data[11]]

    if category in ['SC','ST','OEC']:
        concession_fee = 'YES'
    else:
        concession_fee = 'NO' 

    if data[13] == 'I' :
        nationality = 'INDIAN'
    
    if data[15] == data[16] :
        relationship = ' / FATHER'
    else :
        if data[15] == data[17] :
            relationship = ' / MOTHER'
        else :
            relationship = ''

    DATES = {"1" : "FIRST", "01" : "FIRST", "2" : "SECOND", "02" : "SECOND", \
            "3" : "THIRD", "03" : "THIRD", "4" : "FOURTH", "04" : "FOURTH", \
            "5" : "FIFTH", "05" : "FIFTH", "6" : "SIXTH", "06" : "SIXTH", \
            "7" : "SEVENTH", "07" : "SEVENTH", "8" : "EIGHTH", "08" : "EIGHTH", \
            "9" : "NINTH", "09" : "NINTH", "10" : "TENTH", "11" : "ELEVENTH", \
            "12" : "TWELFTH", "13" : "THIRTEENTH", "14" : "FOURTEENTH", \
            "15" : "FIFTEENTH", "16" : "SIXTEENTH", "17" : "SEVENTEENTH", \
            "18" : "EIGHTEENTH", "19" : "NINETEENTH", "20" : "TWENTIETH", \
            "21" : "TWENTY FIRST", "22" : "TWENTY SECOND", "23" : "TWENTY THIRD", \
            "24" : "TWENTY FOURTH", "25" : "TWENTY FIFTH", "26" : "TWENTY SIXTH", \
            "27" : "TWENTY SEVENTH", "28" : "TWENTY EIGHTH", "29" : "TWENTY NINTH", \
            "30" : "THIRTIETH", "31" : "THIRTY FIRST"}

    MONTHS = {"1" : "JANUARY", "01" : "JANUARY", "2" : "FEBRUARY", \
            "02" : "FEBRUARY", "3" : "MARCH", "03" : "MARCH", "4" : "APRIL", \
            "04" : "APRIL", "5" : "MAY", "05" : "MAY", "6" : "JUNE", "06" : "JUNE", \
            "7" : "JULY", "07" : "JULY", "8" : "AUGUST", "08" : "AUGUST", \
            "9" : "SEPTEMBER", "09" : "SEPTEMBER", "10" : "OCTOBER", \
            "11" : "NOVEMBER", "12" : "DECEMBER"}
    DIGITS = {"1" : "ONE", "2" : "TWO", "3" : "THREE", "4" : "FOUR", \
            "5" : "FIVE", "6" : "SIX", "7" : "SEVEN", "8" : "EIGHT", \
            "9" : "NINE"}
    split_dob = data[9].split("/");
    dob = ''
    if (split_dob[2][0:3] == '199') :
        dob = "  (" + DATES[split_dob[0]] + " " + MONTHS[split_dob[1]] + " NINTEEN NINETY " + DIGITS[split_dob[2][3]] + ")"

    
    values = [common_entries[0],common_entries[1],data[2],data[15]+relationship,data[29],nationality,data[12] + ' / ' + religion,category,data[9] + dob ,common_entries[2],common_entries[3],common_entries[4],common_entries[5],concession_fee,common_entries[6],common_entries[7],common_entries[8],common_entries[9],common_entries[10],common_entries[11],common_entries[12],common_entries[13],common_entries[14]]

    firstrow = []
    firstrow.append(firstrow_entries[0] + tcnumber + common_entries[17])
    firstrow.append(firstrow_entries[1] + "")
    firstrow.append(firstrow_entries[2] + data[1])

    pdf.add_page()
    pdf.set_font("Times", "", 8)
    pdf.cell(one_copy_width,2,title1,0,0,'C')
    pdf.cell(2 * margin);
    pdf.cell(one_copy_width,2,title1,0,1,'C')
    pdf.set_font("Times", "B", 10)
    pdf.cell(one_copy_width,6,title2,0,0,'C')
    pdf.cell(2 * margin);
    pdf.cell(one_copy_width,6,title2,0,1,'C')
    pdf.set_font("Times", "", 8)

    text_length = pdf.get_string_width(firstrow[0]+ firstrow[1]+firstrow[2])
    diff = one_copy_width - text_length
    num_spaces = int(diff / (2 * pdf.get_string_width(" ")))

    firstrow[0] = firstrow[0] + num_spaces * " ";
    firstrow[1] = firstrow[1] + num_spaces * " ";
    row = ""
    for entry in firstrow:
        row = row + entry 
    h = 5.7
    pdf.cell(one_copy_width,h,row)
    pdf.cell(2 * margin);
    pdf.cell(one_copy_width,h,row,0,1)

    number = 1
    for entry in main_entries:
        if number < 10 :
            space = 3
        else : 
            space = 1

        text = str(number) + "." + space * " " + entry
        if (pdf.get_string_width(text + " ") > left_width):
            middle = len(text) / 2
            index = text.find(" ",middle)    
            part1 = text[0:index]
            part2 = 5 * " " + text[index+1:len(text)]
            
            if (pdf.get_string_width(": " + values[number-1]) > right_width)  :
                middle = len(values[number - 1]) / 2
                index = values[number-1].find(" ",middle)    
                value_part1 = ": " + values[number -1][0:index]
                value_part2 = 3 * " " + values[number -1][index+1:len(values[number-1])]
            
                pdf.cell(left_width,h,part1,0,0)
                pdf.cell(right_width + 2* margin,h,value_part1)
                pdf.cell(left_width,h,part1,0,0)
                pdf.cell(right_width ,h,value_part1,0,1)
                pdf.cell(left_width,h,part2,0,0)
                pdf.cell(right_width + 2* margin,h,value_part2)
                pdf.cell(left_width,h,part2,0,0)
                pdf.cell(right_width,h,value_part2,0,1)
            else :
                pdf.cell(left_width,h,part1,0,0)
                pdf.cell(right_width + 2* margin,h,": " + values[number-1],0,0)
                pdf.cell(left_width,h,part1,0,0)
                pdf.cell(right_width,h,": " + values[number-1],0,1)
                pdf.cell(left_width + right_width + 2 * margin,h,part2,0,0)
                pdf.cell(left_width,h,part2,0,1)
        else:    
            if (pdf.get_string_width(": " + values[number-1]) > right_width)  :
                middle = len(values[number - 1]) / 2
                index = values[number-1].find(" ",middle)    
                value_part1 = ": " + values[number -1][0:index]
                value_part2 = 3 * " " + values[number -1][index+1:len(values[number-1])]
            
                pdf.cell(left_width,h,text,0,0)
                pdf.cell(right_width + 2* margin,h,value_part1)
                pdf.cell(left_width,h,text,0,0)
                pdf.cell(right_width ,h,value_part1,0,1)
                pdf.cell(left_width)
                pdf.cell(right_width + 2* margin,h,value_part2)
                pdf.cell(left_width)
                pdf.cell(right_width,h,value_part2,0,1)
            else:
                pdf.cell(left_width,h,text,0,0)
                pdf.cell(right_width + 2 * margin ,h,": " + values[number-1],0,0)
                pdf.cell(left_width,h,text,0,0)
                pdf.cell(right_width,h,": " + values[number-1],0,1)
        number = number + 1
    pdf.ln(3)
    pdf.cell(one_copy_width + 2 * margin,h,place + common_entries[15])
    pdf.cell(one_copy_width,h,place+common_entries[15],0,1)
    pdf.cell(one_copy_width + 2 * margin,h,date + common_entries[9])
    pdf.cell(one_copy_width,h,date + common_entries[9],0,1)
    pdf.ln(3)
    pdf.cell(one_copy_width - margin,h,signatory,0,0,'R')
    pdf.cell(2 * margin);
    pdf.cell(one_copy_width - margin,h,signatory,0,0,'R')

pdf.output('TC.pdf','F')
